//
//  ScheduleShouldAvoidView.m
//  MarryBackEnd
//
//  Created by mac on 2020/8/3.
//  Copyright © 2020 WPP. All rights reserved.
//

#import "ScheduleShouldAvoidView.h"
#import "ScheduleCalendarModel.h"
@interface ScheduleShouldAvoidItemView : BaseIconView

@end

@implementation ScheduleShouldAvoidItemView
- (void)adjustFixedFrame
{
    [super adjustFixedFrame];
    self.leftLab.frame = CGRectMake(94*ADAPTER_WIDTH, 14*ADAPTER_WIDTH, 24*ADAPTER_WIDTH, 24*ADAPTER_WIDTH);
    self.leftLab.layer.cornerRadius = 12*ADAPTER_WIDTH;
    self.leftLab.layer.masksToBounds = YES;
    self.leftLab.textAlignment = NSTextAlignmentCenter;
    self.leftLab.font = kFontRegular14;
    self.leftLab.textColor = WhiteColor;
    
    self.rightLab.textColor = rgba(51, 51, 51, 1);
    self.rightLab.textAlignment = NSTextAlignmentLeft;
    self.rightLab.font = kFontRegular14;
    self.rightLab.numberOfLines = 0;
    self.rightLab.frame = CGRectMake(123*ADAPTER_WIDTH, 19*ADAPTER_WIDTH, 140*ADAPTER_WIDTH, 0);
}
- (void)setIconModel:(BaseIconModel *)iconModel
{
    [super setIconModel:iconModel];
    //data
    if ([self.iconModel.identify isEqualToString:@"should"]) {
        self.leftLab.text = @"宜";
        self.leftLab.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeDownDiagonal andWithSize:self.leftLab.size andWithColors:@[(__bridge id)rgba(113, 239, 156, 1).CGColor,(__bridge id)rgba(22, 194, 131, 1).CGColor]];
    } else {
        self.leftLab.text = @"忌";
        self.leftLab.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeDownDiagonal andWithSize:self.leftLab.size andWithColors:@[(__bridge id)rgba(153, 153, 153, 1).CGColor,(__bridge id)rgba(50, 50, 50, 1).CGColor]];
    }
    self.rightLab.text = iconModel.title;
    //frame
    CGFloat rightLabHeight = [iconModel.title sizeWithMaxWidth:self.rightLab.width andFont:self.rightLab.font].height;
    self.rightLab.height = rightLabHeight;
    self.height = MAX(self.rightLab.bottom, self.leftLab.bottom);
}
@end


@interface ScheduleShouldAvoidView ()
@property(nonatomic,strong)UIImageView *headerIV;
@property(nonatomic,strong)UILabel *dayLab;
@property(nonatomic,strong)UILabel *nongliLab;
@property(nonatomic,strong)UILabel *wanliLab;
@property(nonatomic,strong)NSMutableArray<ScheduleShouldAvoidItemView *> *itemsArray;
@property(nonatomic,strong)BaseAdjustButton *collectionButton;
@property(nonatomic,strong)UIButton *lookButton;
@end

@implementation ScheduleShouldAvoidView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = WhiteColor;
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)createProperty
{
    self.layer.cornerRadius = 6*ADAPTER_WIDTH;
    self.layer.masksToBounds = YES;
}
- (void)createUI
{
    for (NSInteger index = 0; index < 2; index ++) {
        BaseIconModel *iconModel = [[BaseIconModel alloc]init];
        if (index == 0) {
            iconModel.identify = @"should";
            iconModel.title = @"";
        } else {
            iconModel.identify = @"avoid";
            iconModel.title = @"";
        }
        ScheduleShouldAvoidItemView *itemView = [[ScheduleShouldAvoidItemView alloc]initWithFrame:CGRectMake(0, 0, self.width, 0)];
        itemView.iconModel = iconModel;
        [self.itemsArray addObject:itemView];
    }
}
- (void)setCalendarModel:(ScheduleCalendarModel *)calendarModel
{
    _calendarModel = calendarModel;
    self.dayLab.text = [NSString stringWithFormat:@"%ld",calendarModel.day];
    self.nongliLab.text = calendarModel.yinli;
    self.wanliLab.text = calendarModel.wanli;
    self.collectionButton.selected = [calendarModel.isChoice isEqualToString:@"1"] ? YES : NO;
    //frame
    CGFloat orinY = self.headerIV.bottom;
    for (NSInteger index = 0; index < self.itemsArray.count; index ++) {
        ScheduleShouldAvoidItemView *itemView = self.itemsArray[index];
        if (index == 0) {
            itemView.iconModel.title = calendarModel.yi;
        } else {
            itemView.iconModel.title = calendarModel.ji;
        }
        itemView.iconModel = itemView.iconModel;
        [self addSubview:itemView];
        itemView.top = orinY;
        orinY = itemView.bottom;
    }
    
    self.collectionButton.top = orinY + 14*ADAPTER_WIDTH;
    orinY = self.collectionButton.bottom;
    self.height = orinY + 16*ADAPTER_WIDTH;
    self.lookButton.top = self.collectionButton.top;
    !self.refreshUIBlock ?: self.refreshUIBlock ();
}
#pragma mark - click
- (void)clickItemButton:(UIButton *)sender
{
    WK(weakSelf)
    switch (sender.tag) {
        case 0:
        {
            if (self.calendarModel == nil) {
                return;
            }
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            [parameters setValue:self.calendarModel.dateStr forKey:@"choiceTime"];
            [parameters setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
            NSString *url = @"";
            if ([self.calendarModel.isChoice isEqualToString:@"1"]) {
                url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,@"/sysOldCalendar/delete"];
            } else {
                url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,@"/sysOldCalendar/insert"];
            }
            [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:parameters FinishBlock:^(id  _Nonnull responseObject) {
                NSLog(@"%@",responseObject);
                BaseResponse *responModel = [BaseResponse modelWithJSON:responseObject];
                if (responModel.success && responModel.code == 10000) {
                    if ([weakSelf.calendarModel.isChoice isEqualToString:@"1"]) {
                        weakSelf.calendarModel.isChoice = @"2";
                        weakSelf.collectionButton.selected = NO;
                    } else {
                        weakSelf.calendarModel.isChoice = @"1";
                        weakSelf.collectionButton.selected = YES;
                    }
                    !weakSelf.collectionBlock ?: weakSelf.collectionBlock ();
                } else {
                    [SVProgressHUD showWithStatus:responModel.message];
                    [SVProgressHUD dismissWithDelay:1];
                }
            } FailedBlock:^(NSError * _Nonnull error) {
                [SVProgressHUD showWithStatus:@"请检查网络~"];
                [SVProgressHUD dismissWithDelay:1];
            }];
        }
            break;
        case 1:
        {
            HomeADItemEventModel *model = [[HomeADItemEventModel alloc] init];
            model.eventSellerId = [NSString stringWithFormat:@"%@%@",BA_GlobalH5_Domain,@"/hotel?classId=12"];
            model.eventClickId = TT_JUMPTYPE_H5;
            [[TT_JumpManager sharedManager] jumpWithModel:model completion:^(BOOL success, NSString * _Nonnull message) {
            }];
        }
            break;
        default:
            break;
    }
}
#pragma mark - lazy懒加载
- (UIImageView *)headerIV
{
    if (!_headerIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"ScheduleManager_icon"];
        iv.frame = CGRectMake((self.width - 271*ADAPTER_WIDTH) * 0.5, 0, 271*ADAPTER_WIDTH, 59*ADAPTER_WIDTH);
        _headerIV = iv;
    }
    return _headerIV;
}
- (NSMutableArray<ScheduleShouldAvoidItemView *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
- (UILabel *)dayLab
{
    if (!_dayLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.headerIV addSubview:lab];
        lab.font = [UIFont systemFontOfSize:40*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = rgba(255, 66, 66, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(45*ADAPTER_WIDTH, 11*ADAPTER_WIDTH, 55*ADAPTER_WIDTH, 42*ADAPTER_WIDTH);
        _dayLab = lab;
    }
    return _dayLab;
}
- (UILabel *)nongliLab
{
    if (!_nongliLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.headerIV addSubview:lab];
        lab.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = [UIColor blackColor];
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(103*ADAPTER_WIDTH, 15*ADAPTER_WIDTH, 160*ADAPTER_WIDTH, 16*ADAPTER_WIDTH);
        _nongliLab = lab;
    }
    return _nongliLab;
}
- (UILabel *)wanliLab
{
    if (!_wanliLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.headerIV addSubview:lab];
        lab.font = [UIFont systemFontOfSize:13*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = rgba(150, 150, 150, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(103*ADAPTER_WIDTH, 6*ADAPTER_WIDTH + self.nongliLab.bottom, 160*ADAPTER_WIDTH, 16*ADAPTER_WIDTH);
        _wanliLab = lab;
    }
    return _wanliLab;
}
- (BaseAdjustButton *)collectionButton
{
    if (!_collectionButton) {
        BaseAdjustButton *button = [BaseAdjustButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        button.type = layoutTypeHorizontalLeftImageRightTitle;
        button.imageWidth = 17*ADAPTER_WIDTH;
        button.imageHeight = 17*ADAPTER_WIDTH;
        button.title = @"预选吉日";
        button.fontWeight = UIFontWeightRegular;
        button.fontSize = 14*ADAPTER_WIDTH;
        button.marginLeft = 11*ADAPTER_WIDTH;
        button.marginItemX = 5*ADAPTER_WIDTH;
        button.marginRight = 15*ADAPTER_WIDTH;
        button.marginTop = 10*ADAPTER_WIDTH;
        button.marginBottom = 10*ADAPTER_WIDTH;
        [button begin];
        button.left = 12*ADAPTER_WIDTH;
        [button setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"ScheduleManager_collection_cancel"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"ScheduleManager_collection_sure"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(clickItemButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 0;
        button.backgroundColor = rgba(245, 245, 245, 1);
        button.layer.cornerRadius = button.height * 0.5;
        button.layer.masksToBounds = YES;
        _collectionButton = button;
    }
    return _collectionButton;
}
- (UIButton *)lookButton
{
    if (!_lookButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button setTitle:@"查看档期适合婚宴场地" forState:UIControlStateNormal];
        [button setTitleColor:WhiteColor forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        button.tag = 1;
        [button addTarget:self action:@selector(clickItemButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(self.collectionButton.right + 8*ADAPTER_WIDTH, 0, self.width - self.collectionButton.right - 20*ADAPTER_WIDTH, 37*ADAPTER_WIDTH);
        button.layer.cornerRadius = button.height * 0.5;
        button.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeDownDiagonal andWithSize:button.size andWithColors:@[(__bridge id)rgba(255, 146, 92, 1).CGColor,(__bridge id)rgba(255, 46, 46, 1).CGColor]];
        _lookButton = button;
    }
    return _lookButton;
}
@end

